app.controller('RtLocationListCtrl', function($scope, ajax, $modal, toaster) {

    var querier = new Req.GET({
        page_index: 1,
        resource: $scope.resource
    });
    var inserter = new Req.POST({ resource: $scope.resource });
    var deleter = new Req.DELETE({ resource: $scope.resource });
    var updater = new Req.PUT({ resource: $scope.resource });


    $scope.reload = $scope.page.loader('location/rt/{pageIndex}/{pageSize}/page');
    $scope.$on('pageIndex', function(event, p) { $scope.reload(p, $scope.queryMap); });
    $scope.reload(1);
    $scope.queryMap = {};
    $scope.query = function() { $scope.reload(1, $scope.queryMap); };

    // 新增、更新
    $scope.write = function(size, current_data, submit_type) {
        $scope.current_data = angular.copy(current_data);
        var modalInstance = $modal.open({
            templateUrl: 'template/rent/location/writeModal.html?v=571ced3b95',
            controller: 'RtLocationModalCtrl',
            size: size,
            resolve: {
                param: function() {
                    return {
                        "data": $scope.current_data,
                        "type": submit_type
                    };
                }
            }
        });

        modalInstance.result.then(function(param) {
            if (param.type == 'update') {
                updater.fields = param.data;
                updater.id = param.data.id;
                updater.resource = 'location/rt/update';
                $scope.page.mod(updater, $scope.reload);
            } else if (param.type == 'insert') {
                inserter.fields = param.data;
                inserter.resource = 'location/rt/insert';
                $scope.page.add(inserter, $scope.reload);
            }
        });
    };

    //删除
    $scope.del = function(size, current_data, submit_type) {
		var modalInstance = $scope.delModal(size, current_data, submit_type);
        modalInstance.result.then(function(param) {
            if (param.type == 'delete') {
                deleter.id = param.data.id;
                deleter.resource = 'location/rt/' + deleter.id + '/delete';
                $scope.page.del(deleter, $scope.reload);
            }
        });
    };

    $scope.onoff = function(loaction) {
        loaction.recommend = !loaction.recommend;
        updater.fields = loaction;
        updater.id = loaction.id;
        updater.resource = 'location/rt/update';
        ajax.call(updater).then(function(result) {
            if (result.code == 200) {
                toaster.pop('success', '成功', '修改推荐地点成功。');
            } else {
                toaster.pop('error', '错误', '设置失败。');
            }
        }, function() {
            toaster.pop('error', '错误', '设置失败。');
        });
    };

});
